﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Web.Script.Serialization;
using System.Reflection;
using HF.Software.ClassLib;
using HF.Software.Entities;

namespace HF.Software
{
    /// <summary>
    /// hef创建于2015.02.04
    /// 卖品配置文件读写
    /// </summary>
    public partial class frmPos : Form
    {

        #region event

        /// <summary>
        /// 验证打印端口是否为空
        /// 为空 打印设备和小票宽度可用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbPrintPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cmbPrinter.Enabled = true;
            this.cmbTicketWidth.Enabled = true;

            if (string.IsNullOrWhiteSpace(this.cmbPrintPort.Text.Trim())) return;

            this.cmbPrinter.Enabled = false;
            this.cmbTicketWidth.Enabled = false;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (null == this.oConfig) return;

            IniModelDataCollection oDataCollection = new IniModelDataCollection();
            IniWrite oIniWrite = new IniWrite();

            POS oPos = this.oConfig.POS;

            try
            {
                PropertyInfo[] oFields = oPos.GetType().GetProperties();

                foreach (PropertyInfo oInfo in oFields)
                {
                    IniModel oConfigIni = new IniModel();
                    oConfigIni.IN_Section = "POS";
                    oConfigIni.IN_Key = oInfo.Name;
                    oConfigIni.IN_Value = Convert.ToString(oInfo.GetValue(oPos, null));

                    switch (oConfigIni.IN_Key.Trim())
                    {
                        case "PrinterNo"://票机类别编号
                            oConfigIni.IN_Value = this.cmbTicketNum.SelectedValue.ToString().Trim();
                            break;

                        case "DisplayNo"://客显类别编号
                            oConfigIni.IN_Value = this.cmbClientNum.SelectedValue.ToString().Trim();
                            break;

                        case "LPTPort"://小票打印端口
                            oConfigIni.IN_Value = this.cmbPrintPort.SelectedValue.ToString().Trim();
                            break;

                        case "Printer"://小票打印设备
                            oConfigIni.IN_Value = "";
                            if (!string.IsNullOrWhiteSpace(this.cmbPrinter.Text))
                                oConfigIni.IN_Value = this.cmbPrinter.Text.Trim();
                            break;

                        case "PrintWidth"://小票宽度
                            oConfigIni.IN_Value = this.cmbTicketWidth.SelectedValue.ToString().Trim();
                            break;

                        case "COMPort"://客显连接端口
                            oConfigIni.IN_Value = this.cmbClientPort.SelectedValue.ToString().Trim();
                            break;

                        case "BaudRate"://波特率
                            oConfigIni.IN_Value = this.txtBaudRate.Text.Trim();
                            break;

                        case "StoreID"://经营分店
                            oConfigIni.IN_Value = "";
                            if (null != this.cmbBranch.SelectedValue)
                                oConfigIni.IN_Value = this.cmbBranch.Text + "," + this.cmbBranch.SelectedValue.ToString().Trim();
                            break;

                        case "ShowPos"://卖品
                            if (0 == this.lvGoods.Items.Count
                                || 0 == this.lvGoods.CheckedItems.Count)
                                oConfigIni.IN_Value = "";
                            else
                            {
                                string sLvGoods = "";

                                for (int i = 0; i < this.lvGoods.CheckedItems.Count; i++)
                                {
                                    if (this.lvGoods.CheckedItems[i].Checked)
                                    {
                                        if (string.IsNullOrWhiteSpace(sLvGoods))
                                        {
                                            sLvGoods = this.lvGoods.CheckedItems[i].Text;
                                        }
                                        else
                                        {
                                            sLvGoods = sLvGoods + "," + this.lvGoods.CheckedItems[i].Text;
                                        }
                                    }
                                }

                                oConfigIni.IN_Value = sLvGoods;
                            }
                            break;

                        case "ShowSpNo"://是否显示卖品编号
                            if (this.chkIsShowNum.Checked)
                                oConfigIni.IN_Value = "T";
                            else
                                oConfigIni.IN_Value = "F";
                            break;
                    }
                    //this.oLog.fnLog("201503110923卖品更新节点：" + ConfigIni.IN_Key + ":" + ConfigIni.IN_Value);
                    oDataCollection.Add(oConfigIni);
                }

                foreach (IniModel oIniModel in oDataCollection)
                {
                    //this.oLog.fnLog("201503110920卖品更新节点：" + oIniModel.IN_Key + ":" + oIniModel.IN_Value);
                    oIniWrite.Write(oIniModel.IN_Section, oIniModel.IN_Key, oIniModel.IN_Value);
                }
                MessageBox.Show("卖品更新成功！", "提示");
            }
            catch (Exception ex)
            {
                this.oLog.fnLog("201502101634卖品更新失败：" + ex.ToString());
                MessageBox.Show("卖品更新失败！", "提示");
            }
        }

        /// <summary>
        /// 设置卖品编号是否显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkIsShowNum_CheckedChanged(object sender, EventArgs e)
        {
            if (!this.chkIsShowNum.Checked)
            {
                this.lvGoods.Columns[0].Width = 0;
            }
            else
            {
                this.lvGoods.Columns[0].Width = 100;
            }
        }

        #endregion

        #region fun

        #region 初始化下拉列表数据

        /// <summary>
        /// 初始化下拉列表数据
        /// </summary>
        private void fnDataBind()
        {
            #region 票机类别编号

            List<ClsComboBoxItem> lstTicketNum = new List<ClsComboBoxItem>();

            ClsComboBoxItem oCmbTicketNum1 = new ClsComboBoxItem();
            oCmbTicketNum1.Value = "0";
            oCmbTicketNum1.Text = "0.一般";
            lstTicketNum.Add(oCmbTicketNum1);

            ClsComboBoxItem oCmbTicketNum2 = new ClsComboBoxItem();
            oCmbTicketNum2.Value = "1";
            oCmbTicketNum2.Text = "1.诺捷";
            lstTicketNum.Add(oCmbTicketNum2);

            ClsComboBoxItem oCmbTicketNum3 = new ClsComboBoxItem();
            oCmbTicketNum3.Value = "2";
            oCmbTicketNum3.Text = "2.北洋串口";
            lstTicketNum.Add(oCmbTicketNum3);

            ClsComboBoxItem oCmbTicketNum4 = new ClsComboBoxItem();
            oCmbTicketNum4.Value = "3";
            oCmbTicketNum4.Text = "3.其它";
            lstTicketNum.Add(oCmbTicketNum4);

            ClsComboBoxItem oCmbTicketNum5 = new ClsComboBoxItem();
            oCmbTicketNum5.Value = "9";
            oCmbTicketNum5.Text = "9.NEC";
            lstTicketNum.Add(oCmbTicketNum5);

            this.cmbTicketNum.DataSource = lstTicketNum;
            this.cmbTicketNum.DisplayMember = "Text";
            this.cmbTicketNum.ValueMember = "Value";

            this.cmbTicketNum.SelectedValue = this.oConfig.POS.PrinterNo;

            #endregion

            #region  客显类别编号

            List<ClsComboBoxItem> lstClientNum = new List<ClsComboBoxItem>();

            ClsComboBoxItem oClientNum1 = new ClsComboBoxItem();
            oClientNum1.Value = "0";
            oClientNum1.Text = "0.拍档";
            lstClientNum.Add(oClientNum1);

            ClsComboBoxItem oClientNum2 = new ClsComboBoxItem();
            oClientNum2.Value = "1";
            oClientNum2.Text = "1.诺捷";
            lstClientNum.Add(oClientNum2);

            ClsComboBoxItem oClientNum3 = new ClsComboBoxItem();
            oClientNum3.Value = "2";
            oClientNum3.Text = "2.其它";
            lstClientNum.Add(oClientNum3);

            ClsComboBoxItem oClientNum4 = new ClsComboBoxItem();
            oClientNum4.Value = "9";
            oClientNum4.Text = "9.双屏";
            lstClientNum.Add(oClientNum4);

            this.cmbClientNum.DataSource = lstClientNum;
            this.cmbClientNum.DisplayMember = "Text";
            this.cmbClientNum.ValueMember = "Value";

            this.cmbClientNum.SelectedValue = this.oConfig.POS.DisplayNo;

            #endregion

            #region 客显连接端口

            List<ClsComboBoxItem> lstClientPort = new List<ClsComboBoxItem>();

            ClsComboBoxItem oClientPort1 = new ClsComboBoxItem();
            oClientPort1.Value = "COM1";
            oClientPort1.Text = "COM1";
            lstClientPort.Add(oClientPort1);

            ClsComboBoxItem oClientPort2 = new ClsComboBoxItem();
            oClientPort2.Value = "COM2";
            oClientPort2.Text = "COM2";
            lstClientPort.Add(oClientPort2);

            ClsComboBoxItem oClientPort3 = new ClsComboBoxItem();
            oClientPort3.Value = "COM3";
            oClientPort3.Text = "COM3";
            lstClientPort.Add(oClientPort3);

            ClsComboBoxItem oClientPort4 = new ClsComboBoxItem();
            oClientPort4.Value = "COM4";
            oClientPort4.Text = "COM4";
            lstClientPort.Add(oClientPort4);

            this.cmbClientPort.DataSource = lstClientPort;
            this.cmbClientPort.DisplayMember = "Text";
            this.cmbClientPort.ValueMember = "Value";

            this.cmbClientPort.SelectedValue = this.oConfig.POS.COMPort;

            #endregion

            #region 小票打印端口

            List<ClsComboBoxItem> lstPrintPort = new List<ClsComboBoxItem>();

            ClsComboBoxItem oPrintPort0 = new ClsComboBoxItem();
            oPrintPort0.Value = "";
            oPrintPort0.Text = "";
            lstPrintPort.Add(oPrintPort0);

            ClsComboBoxItem oPrintPort1 = new ClsComboBoxItem();
            oPrintPort1.Value = "LPT1";
            oPrintPort1.Text = "LPT1";
            lstPrintPort.Add(oPrintPort1);

            ClsComboBoxItem oPrintPort2 = new ClsComboBoxItem();
            oPrintPort2.Value = "LPT2";
            oPrintPort2.Text = "LPT2";
            lstPrintPort.Add(oPrintPort2);

            ClsComboBoxItem oPrintPort3 = new ClsComboBoxItem();
            oPrintPort3.Value = "LPT3";
            oPrintPort3.Text = "LPT3";
            lstPrintPort.Add(oPrintPort3);

            ClsComboBoxItem oPrintPort4 = new ClsComboBoxItem();
            oPrintPort4.Value = "COM1";
            oPrintPort4.Text = "COM1";
            lstPrintPort.Add(oPrintPort4);

            ClsComboBoxItem oPrintPort5 = new ClsComboBoxItem();
            oPrintPort5.Value = "COM2";
            oPrintPort5.Text = "COM2";
            lstPrintPort.Add(oPrintPort5);

            ClsComboBoxItem oPrintPort6 = new ClsComboBoxItem();
            oPrintPort6.Value = "COM3";
            oPrintPort6.Text = "COM3";
            lstPrintPort.Add(oPrintPort6);

            ClsComboBoxItem oPrintPort7 = new ClsComboBoxItem();
            oPrintPort7.Value = "COM4";
            oPrintPort7.Text = "COM4";
            lstPrintPort.Add(oPrintPort7);

            ClsComboBoxItem oPrintPort8 = new ClsComboBoxItem();
            oPrintPort8.Value = "USB";
            oPrintPort8.Text = "USB";
            lstPrintPort.Add(oPrintPort8);

            this.cmbPrintPort.DataSource = lstPrintPort;
            this.cmbPrintPort.DisplayMember = "Text";
            this.cmbPrintPort.ValueMember = "Value";

            this.cmbPrintPort.SelectedValue = this.oConfig.POS.LPTPort;

            #endregion

            #region 打印机列表

            List<ClsComboBoxItem> lstPrinter = new List<ClsComboBoxItem>();

            ClsComboBoxItem oPosPrinter = new ClsComboBoxItem();
            oPosPrinter.Value = "";
            oPosPrinter.Text = "";
            lstPrinter.Add(oPosPrinter);

            foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                oPosPrinter = new ClsComboBoxItem();
                oPosPrinter.Value = sPrint;
                oPosPrinter.Text = sPrint;
                lstPrinter.Add(oPosPrinter);
            }

            this.cmbPrinter.DataSource = lstPrinter;
            this.cmbPrinter.DisplayMember = "Text";
            this.cmbPrinter.ValueMember = "Value";

            this.cmbPrinter.SelectedValue = this.oConfig.POS.Printer;

            #endregion

            #region 小票宽度

            List<ClsComboBoxItem> lstTicketWidth = new List<ClsComboBoxItem>();

            ClsComboBoxItem oTicketWidth0 = new ClsComboBoxItem();
            oTicketWidth0.Value = "80";
            oTicketWidth0.Text = "80";
            lstTicketWidth.Add(oTicketWidth0);

            ClsComboBoxItem oTicketWidth1 = new ClsComboBoxItem();
            oTicketWidth1.Value = "58";
            oTicketWidth1.Text = "58";
            lstTicketWidth.Add(oTicketWidth1);

            this.cmbTicketWidth.DataSource = lstTicketWidth;
            this.cmbTicketWidth.DisplayMember = "Text";
            this.cmbTicketWidth.ValueMember = "Value";

            this.cmbTicketWidth.SelectedValue = this.oConfig.POS.PrintWidth;

            #endregion

            #region 经营分店

            //StoreDataCollection oLstStoreData = new StoreDataCollection();//Store.GetList();

            //List<clsComboBoxItem> lstBranch = new List<clsComboBoxItem>();

            //foreach (StoreData oData in oLstStoreData)
            //{
            //    clsComboBoxItem oCmbBranch = new clsComboBoxItem();
            //    oCmbBranch.Value = oData.STOREID;
            //    oCmbBranch.Text = oData.STORENAME;
            //    lstBranch.Add(oCmbBranch);
            //}

            //this.cmbBranch.DataSource = lstBranch;
            //this.cmbBranch.DisplayMember = "Text";
            //this.cmbBranch.ValueMember = "Value";

            //this.cmbBranch.SelectedValue = this.oConfig.POS.StoreID;

            if (null != this.oConfig && !string.IsNullOrWhiteSpace(this.oConfig.POS.StoreID))
            {
                string[] sArrBranch = this.oConfig.POS.StoreID.Trim().Split(',');

                if (null != sArrBranch && 1 < sArrBranch.Length)
                {
                    List<ClsComboBoxItem> lstBranch = new List<ClsComboBoxItem>();

                    ClsComboBoxItem oCmbBranch = new ClsComboBoxItem();
                    oCmbBranch.Value = sArrBranch[1].ToString();
                    oCmbBranch.Text = sArrBranch[0].ToString();
                    lstBranch.Add(oCmbBranch);

                    this.cmbBranch.DataSource = lstBranch;
                    this.cmbBranch.DisplayMember = "Text";
                    this.cmbBranch.ValueMember = "Value";
                    this.cmbBranch.SelectedIndex = 0;
                }
            }

            #endregion
        }

        #endregion

        #region 显示卖品

        /// <summary>
        /// 显示卖品
        /// </summary>
        private void fnLstViewDataWrite()
        {
            #region 卖品

            ListViewItem oLstItem = null;

            string[] sArrGoods = this.oConfig.POS.ShowPos.Split(',');

            if (!string.IsNullOrWhiteSpace(this.sGoods))
            {
                this.oSerializer = new JavaScriptSerializer();

                List<ClsSpData> lstGoodsData = this.oSerializer.Deserialize<List<ClsSpData>>(this.sGoods);

                if (null != lstGoodsData && 0 < lstGoodsData.Count)
                {
                    for (int i = 0; i < lstGoodsData.Count; i++)
                    {
                        oLstItem = new ListViewItem(lstGoodsData[i].SPNAME);
                        oLstItem.SubItems.Add(lstGoodsData[i].SPNO);
                        this.lvGoods.Items.Add(oLstItem);
                    }
                }

                if (0 < this.lvGoods.Items.Count && 0 < sArrGoods.Length)
                {
                    for (int j = 0; j < lstGoodsData.Count; j++)
                    {
                        for (int g = 0; g < sArrGoods.Length; g++)
                        {
                            if (lstGoodsData[j].SPNAME == sArrGoods[g].ToString())
                                this.lvGoods.CheckedItems[j].Checked = true;
                        }
                    }
                }
            }//if
            else
            {
                if (null != sArrGoods && 0 < sArrGoods.Length)
                {
                    for (int k = 0; k < sArrGoods.Length; k++)
                    {
                        if (string.IsNullOrWhiteSpace(sArrGoods[k].ToString())) continue;

                        oLstItem = new ListViewItem(sArrGoods[k].ToString());
                        this.lvGoods.Items.Add(oLstItem);
                    }
                }
            }//end if

            #endregion
        }

        #endregion

        #endregion

        #region create

        public frmPos()
        {
            InitializeComponent();
        }

        public frmPos(string _sBranch, string _sGoods)
            : this()
        {
            this.oLog.fnLog("201502281511分店数据：" + _sBranch + "，卖品数据：" + _sGoods);
            this.sBranch = _sBranch;
            this.sGoods = _sGoods;
        }

        private void frmPos_Load(object sender, EventArgs e)
        {
            if (null == this.oConfig) return;

            this.fnDataBind();

            this.txtBaudRate.Text = this.oConfig.POS.BaudRate;

            this.chkIsShowNum.Checked = this.oConfig.POS.ShowSpNo;

            //显示卖品
            this.fnLstViewDataWrite();

            #region 分店

            if (string.IsNullOrWhiteSpace(this.sBranch)) return;

            try
            {
                this.sBranch = this.sBranch.Replace("_", ",");

                this.oSerializer = new JavaScriptSerializer();

                List<ClsStoreData> lstBranchData = this.oSerializer.Deserialize<List<ClsStoreData>>(this.sBranch);

                if (null == lstBranchData && 0 == lstBranchData.Count) return;

                this.cmbBranch.DataSource = lstBranchData;
                this.cmbBranch.DisplayMember = "STORENAME";
                this.cmbBranch.ValueMember = "STOREID";

                this.cmbBranch.SelectedIndex = 0;

                if (this.oConfig.POS.StoreID.Split(',').Length > 1)
                    this.cmbBranch.SelectedValue = this.oConfig.POS.StoreID.Split(',')[1];
            }
            catch (Exception ex)
            {
                this.oLog.fnLog("201503111128序列化分店异常：" + ex.ToString());
            }

            #endregion

        }

        #endregion

        #region var

        private string sBranch = "";
        private string sGoods = "";
        private AppConfig oConfig = new AppConfig();
        private ClsLog oLog = new ClsLog(Application.StartupPath + @"\log\set\" + DateTime.Now.ToString("yyyyMMddHH") + ".txt");
        private JavaScriptSerializer oSerializer = null;

        #endregion

    }
}
